package cn.jly.spring.boot.cloud.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.util.Arrays;

/**
 * @author lanyangji
 * @description 日志切面
 * @date 2020/11/23 下午 8:35
 */
@Slf4j
@Component
@Aspect
@Order(1)
public class LogAspect {

    @Pointcut("execution(* cn.jly.spring.boot.cloud.controller.*.*(..))")
    public void expression() {}

    @Before(value = "expression()")
    public void before(JoinPoint joinPoint) {
        log.info("LogAspect 请求入参 -> {}", Arrays.toString(joinPoint.getArgs()));
        log.info("LogAspect 方法名 -> {}", joinPoint.getSignature().getName());
        log.info("LogAspect 所在实体类-> {}", joinPoint.getSignature().getDeclaringTypeName());
    }

    @AfterReturning(value = "expression()", returning = "result")
    public void afterReturn(Object result){
        log.info("LogAspect 返回值 -> {}", result);
    }

    @AfterThrowing(value = "expression()", throwing = "ex")
    public void afterThrow(Exception ex) {
        log.info("LogAspect 异常信息为 -> {}", ex.getMessage());
    }

    @After(value = "expression()")
    public void after() {
        log.info("LogAspect after");
    }
}
